// const baseUrl = 'http://localhost:8080';
// const baseWebsocketUrl = 'ws://localhost:8080';

// const baseUrl = 'http://192.168.0.123:8080';
// const baseWebsocketUrl = 'ws://192.168.0.123:8080';

const baseUrl = 'https://yisutime.com';
const baseWebsocketUrl = 'wss://yisutime.com';

let token = null;

export function setToken(t) {
  token = t;
}

export function request(params) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}${params.url}`,
      data: params.data,
      header: {
        'X-Auth-Token': token,
        'content-type': 'application/json'
      },
      timeout: params.timeout,
      method: params.method,
      dataType: params.dataType,
      responseType: params.responseType,
      success: resp => {
        if (resp.data === null) {
          reject(resp.data);
        } else if (resp.data.code === 401) {
          wx.reLaunch({
            url: '/pages/loading/loading',
          });
        } else if (resp.data.code !== 200) {
          reject(resp.data)
        }
        resolve(resp.data.data);
      },
      fail: err => {
        console.log(err)
        reject(err);
      },
      complete: params.complete
    });
  });
}

export { baseUrl, baseWebsocketUrl };
